package com.hyny.framework.base.redis;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Redis工具类
 */
@Component
@Data
@NoArgsConstructor
public class RedisUtil {
    @Value("${spring.redis.host}")
    private String host = "";
    @Value("${spring.redis.port}")
    private int port = 0;
    private int expire = 0;
    @Value("${spring.redis.timeout}")
    private int timeout = 0;
    @Value("${spring.redis.password}")
    private String password = "";

    private static JedisPool jedisPool = null;

    /**
     * 初始化
     * @return
     */
    @Bean
    public JedisPool initJedisPool() {

        if (jedisPool == null) {
            if (password != null && !"".equals(password)) {
                jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout, password);
            } else if (timeout != 0) {
                jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout);
            } else {
                jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
            }
        }
        return jedisPool;
    }
    /**
     * 获取jedis对象，并指定dbIndex
     *
     * @param dbIndex
     */
    public Jedis getJedis(Integer dbIndex) {
        Jedis jedis = jedisPool.getResource();
        if (dbIndex == null || dbIndex > 15 || dbIndex < 0) {
            dbIndex = 0;
        }
        jedis.select(dbIndex);
        return jedis;
    }
    /**
     * key是否存在
     *
     * @param key
     * @param dbIndex
     */
    public Boolean existsKey(String key, int dbIndex) {
        try (Jedis jedis = getJedis(dbIndex)) {
            return jedis.exists(key);
        }
    }


    /**
     * 添加缓存
     * @param key 键
     * @param value 值
     * @param dbIndex  库索引
     * @return
     */
    public String setString(String key, String value, int dbIndex) {
        String String;
        try (Jedis jedis = getJedis(dbIndex)) {
            String = jedis.set(key, value);
        }
        return String;
    }
    /**
     * 超时时间
     */
    public void setStringExpire(String key, String value, long expire, int dbIndex) {
        try (Jedis jedis = getJedis(dbIndex)) {
            if (expire != 0) {
                jedis.set(key, value);
                jedis.expire(key, expire);
            }
        }
    }


    public void removeString(String key, int dbIndex) {
        try (Jedis jedis = getJedis(dbIndex)) {
            jedis.del(key);
        }
    }



    public String getString(String key, int dbIndex) {
        Jedis jedis = getJedis(dbIndex);
        String value;
        try {
            value = jedis.get(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    public void removeString(RedisEnum redisEnum) {
        try (Jedis jedis = getJedis(redisEnum.getDbIndex())) {
            jedis.del(redisEnum.getKey());
        }
    }

    public void removeString(RedisEnum redisEnum,String key) {
        try (Jedis jedis = getJedis(redisEnum.getDbIndex())) {
            jedis.del(redisEnum.getKey()+key);
        }
    }

    public void setStringExpire(RedisEnum redisEnum ,String value, long expire) {
        try (Jedis jedis = getJedis(redisEnum.getDbIndex())) {
            if (expire != 0) {
                jedis.set(redisEnum.getKey(), value);
                jedis.expire(redisEnum.getKey(), expire);
            }
        }
    }

    public void setStringExpire(RedisEnum redisEnum,String key ,String value, long expire) {
        try (Jedis jedis = getJedis(redisEnum.getDbIndex())) {
            if (expire != 0) {
                jedis.set(redisEnum.getKey()+key, value);
                jedis.expire(redisEnum.getKey()+key, expire);
            }
        }
    }

    public String getString(RedisEnum redisEnum) {
        Jedis jedis = getJedis(redisEnum.getDbIndex());
        String value;
        try {
            value = jedis.get(redisEnum.getKey());
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    public String getString(RedisEnum redisEnum,String key) {
        Jedis jedis = getJedis(redisEnum.getDbIndex());
        String value;
        try {
            value = jedis.get(redisEnum.getKey()+key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }
    




}
